package com.siao.utils
{
	import flash.utils.ByteArray;
	
	/**
	 * BASE64编码和解码
	 * @author SiaoLeon
	 */
	public class Base64
	{
		private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		
		/**
		 * 对指定的字符串进行编码
		 * @param data 需要编码的字符串
		 * @return BASE64编码之后的字符串
		 */
		public static function encode(data:String):String
		{
			var bytes:ByteArray = new ByteArray();
			bytes.writeUTFBytes(data);
			
			return encodeByteArray(bytes);
		}
		
		/**
		 * 对指定的字节流进行编码
		 * @param data 需要编码的字节流
		 * @return BASE64编码之后的字符串
		 */
		public static function encodeByteArray(data:ByteArray):String
		{
			var output:String = "";
			
			var dataBuffer:Array;
			var outputBuffer:Array = new Array(4);
			
			data.position = 0;
			
			while (data.bytesAvailable > 0)
			{
				dataBuffer = new Array();
				
				for (var i:uint = 0; i < 3 && data.bytesAvailable > 0; i++)
				{
					dataBuffer[i] = data.readUnsignedByte();
				}
				
				outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2;
				outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4);
				outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6);
				outputBuffer[3] = dataBuffer[2] & 0x3f;
				
				for (var j:uint = dataBuffer.length; j < 3; j++)
				{
					outputBuffer[j + 1] = 64;
				}
				
				for (var k:uint = 0; k < outputBuffer.length; k++)
				{
					output += BASE64_CHARS.charAt(outputBuffer[k]);
				}
			}
			
			return output;
		}
		
		/**
		 * 对uint数组表示的字节流进行编码
		 * @param data 一个uint组成的数组表示的字节流
		 * @return BASE64编码之后的字符串
		 */
		public static function encodeUintByteArray(data:Array):String
		{
			var bytes:ByteArray = new ByteArray();
			
			for each (var u:uint in data)
			{
				bytes.writeByte(int(u));
			}
			bytes.position = 0;
			
			return encodeByteArray(bytes);
		}
		
		/**
		 * 从BASE64编码的字符串解码
		 * @param data BASE64 编码的字符串
		 * @return 字符串
		 */
		public static function decode(data:String):String
		{
			var bytes:ByteArray = decodeToByteArray(data);
			
			return bytes.readUTFBytes(bytes.length);
		}
		
		/**
		 * 获取BASE64字符串形式编码的数据
		 * @param data BASE64编码的字符串
		 * @return 编码之后的字节流（每个字节用一个uint表示，整个字节流使用Array形式）
		 */
		public static function decodeToUintByteArray(data:String):Array
		{
			var result:Array = new Array();
			var bytes:ByteArray = decodeToByteArray(data);
			bytes.position = 0;
			
			var u:uint = 0;
			
			while (bytes.bytesAvailable)
			{
				u = bytes.readByte();
				result.push(u);
			}
			
			return result;
		}
		
		/**
		 * 获取BASE64字符串形式编码的数据
		 * @param data BASE64 编码的字符串
		 * @return 解码之后的字节流
		 */
		public static function decodeToByteArray(data:String):ByteArray
		{
			var output:ByteArray = new ByteArray();
			
			var dataBuffer:Array = new Array(4);
			var outputBuffer:Array = new Array(3);
			
			for (var i:uint = 0; i < data.length; i += 4)
			{
				for (var j:uint = 0; j < 4 && i + j < data.length; j++)
				{
					dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j));
				}
				
				outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 0x30) >> 4);
				outputBuffer[1] = ((dataBuffer[1] & 0x0f) << 4) + ((dataBuffer[2] & 0x3c) >> 2);
				outputBuffer[2] = ((dataBuffer[2] & 0x03) << 6) + dataBuffer[3];
				
				for (var k:uint = 0; k < outputBuffer.length; k++)
				{
					if (dataBuffer[k + 1] == 64)
						break;
					output.writeByte(outputBuffer[k]);
				}
			}
			
			output.position = 0;
			
			return output;
		}
	
	}
}
